/**
 *====================================================
 * 文件名称: SupervisionContractServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.SupervisionContractEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: SupervisionContractServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(SupervisionContractService.class)
public class SupervisionContractServiceImpl implements SupervisionContractService {

	@Override
	public Long createSupervisionContract(SupervisionContractEntity entity) {
		entity.setContractUploadDate(DateUtil.todayInt());
		entity.setContractNumber(SettingConstants.PRE_NUMBER_SUPERVISION_CONTRACT + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getSupervisionContractId();
	}

	@Override
	public void updateSupervisionContract(SupervisionContractEntity entity) {
		SupervisionContractEntity persist = inquireSupervisionContractEntity(entity.getSupervisionContractId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public SupervisionContractEntity inquireSupervisionContractEntity(Long supervisionContractId) {
		return PersistUtil.get(SupervisionContractEntity.class, supervisionContractId);
	}

	@Override
	public SupervisionContractEntity inquireFinanceIdSupervisionContract(Long financeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(SupervisionContractEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		List<SupervisionContractEntity> list = PersistUtil.get(criteria);
		if (list.size() > 1) {
			throw new BusinessException("存在多份的融资监管合同");
		}
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
